%% this code is to produce figure 7
%% Input
clear

rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta_0 = 0.09;
rhob = 3.69;
rhoo = 4.38;

% Data
F_tot =(331*0.6/20)*354.500; % population* average loan size (Billion)
Nb = 8;
Nm = 11;
Nn = 24;
%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

F_tot_5 = 7.017*214 % million
F_tot_95 = 35.883*400 % million
%% Counterfactual: Warehouse duration

for i = 1:24
    eta=0.01*i
    d_eta(i)=0.01*i;
    [rno(i),sno(i),rnb(i),snb(i),rm(i),sbm(i),rb(i),sb(i), rhob(i), sbw(i), sow(i),~,~] = Solve_local(F,sigma,psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo);
 
end
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,~,~] = Solve_local(F,sigma,psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta_0,rhoo);

%% change in mortgage credit provision
shadow_share1 = sno1*Nn*sow1+snb1*Nn*sbw1*Nb
bank_share1 = sb1*Nb+sbm1*Nm
outside1 = 1-shadow_share1-bank_share1

shadow_share2 = sno.*Nn.*sow+snb.*Nn.*sbw.*Nb
bank_share2 = sb.*Nb+sbm.*Nm
outside2 = 1-shadow_share2-bank_share2

d_sb = (shadow_share2-shadow_share1)*F_tot
d_b = (bank_share2-bank_share1)*F_tot
d_credit = (outside1-outside2)*F_tot

           
figure
h1 = bar(d_eta,d_sb,'b')
set(h1,'FaceColor', [0 0 0])
hold on
h2 = bar(d_eta, d_b)
set(h2,'FaceColor', [0.5 0.5 0.5])
h3 = bar(d_eta, d_credit,'r')
set(h3,'FaceColor', [0.8 0.8 0.8])

y.label = ylabel('Change in Mortgage Supply (Billion)')
x.label = xlabel('Warehouse Duration (1x = 3 months)')

set(gca, 'xtick',[0 0.045 0.09 0.135 0.18 0.225],'XtickLabel',{'-1x' '-0.5x' '+0x' '+1.5x' '+2x' '+2.5x' '+3x'  } , 'fontsize', 12)
legend({'Shadow Bank','Bank', 'Net'},'Location', 'southwest')

saveas(gcf,'figure7a.png')




%% Consumer surplus: 
annuity_factor = (1-(1+0.04)^(-3))/0.04;
surplus_base =0.01*F_tot*annuity_factor*((log(Nb*exp(-alpha*rb1+qb)+Nm*exp(-alpha*rm1+qb)+Nn*sow1*exp(-alpha*rno1+qn)+Nn*sbw1*Nb*exp(-alpha*rnb1+qn))/alpha));
surplus=0.01*F_tot*annuity_factor*( (log(Nb.*exp(-alpha.*rb+qb)+Nm.*exp(-alpha.*rm+qb)+Nn.*sow.*exp(-alpha.*rno+qn)+Nn.*sbw.*Nb.*exp(-alpha.*rnb+qn))./alpha));

d_surplus = surplus-surplus_base

figure 
h1=bar(d_eta,d_surplus)
set(h1,'FaceColor', [0 0 0])

hold on

x.label = xlabel('Warehouse Duration (1x = 3 months)')
ylabel('\Delta Consumer Surplus (Billion)')
%ylim([0.9 1.04])

set(gca, 'xtick',[0 0.045 0.09 0.135 0.18 0.225],'XtickLabel',{'-1x' '-0.5x' '+0x' '+1.5x' '+2x' '+2.5x' '+3x'  } , 'fontsize', 12)

saveas(gcf,'figure7b.png')




%%  Dollar flow from banks to shadow banks
wb1 = (1-sow1)*Nn*snb1;
wb2 = (1-sow).*Nn.*snb;
d_wb_vol = (wb2-wb1).*F_tot


figure 
h1 = bar(d_eta,d_wb_vol)
set(h1,'FaceColor', [0 0 0])

hold
x.label = xlabel('Warehouse Duration (1x = 3 months)')
y.label = ylabel('\Delta Dollar Flow through Shadow Banks (Billion)')

set(gca, 'xtick',[0 0.045 0.09 0.135 0.18 0.225],'XtickLabel',{'-1x' '-0.5x' '+0x' '+1.5x' '+2x' '+2.5x' '+3x'  } , 'fontsize', 12)

saveas(gcf,'figure7c.png')


%% profit

% Shadow bank mortgage profit
profit_rn1=(rnb1-eta_0*rhob1-k)*snb1*Nn*sbw1*Nb+(rno1-eta_0*rhoo-k)*sno1*sno1*Nn*sow1;
profit_rn2=(rnb-d_eta.*rhob-k).*snb.*Nn.*sbw.*Nb+(rno-d_eta.*rhoo-k).*sno.*sno.*Nn.*sow;
dp_rn = (profit_rn2-profit_rn1).*F_tot

% bank mortgage profit
profit_rb1 = (rb1-psim)*sb1*Nb+(rm1-psim)*sbm1*Nm;
profit_rb2 = (rb-psim).*sb.*Nb+(rm-psim).*sbm.*Nm;
dp_rb = (profit_rb2-profit_rb1)*F_tot
% bank warehouse lending profit
profit_wb1= eta_0*(rhob1-psiw)*sbw1*Nb*Nn*snb1;
profit_wb2= d_eta.*(rhob-psiw).*sbw.*Nb.*Nn.*snb;
dp_wb = (profit_wb2-profit_wb1)*F_tot


         
figure
h1 = bar(d_eta,dp_rn,'b')
set(h1,'FaceColor', [0 0 0])
hold on
h2 = bar(d_eta, dp_rb)
set(h2,'FaceColor', [0.5 0.5 0.5])
h3 = bar(d_eta, dp_wb,'r')
set(h3,'FaceColor', [0.8 0.8 0.8])

y.label = ylabel('\Delta Profit (Billion)')
x.label = xlabel('Warehouse Duration (1x = 3 months)')

set(gca, 'xtick',[0 0.045 0.09 0.135 0.18 0.225],'XtickLabel',{'-1x' '-0.5x' '+0x' '+1.5x' '+2x' '+2.5x' '+3x'  } , 'fontsize', 12)
legend({'Shadow Bank Mortgage','Bank Mortgage','Bank Warehouse Lending'},'Location', 'southwest')

saveas(gcf,'figure7d.png')
